#!/bin/bash

SQLUSER=root
SQLPWD=
SQLSERVER=localhost
SQLSCHEMA=turnlink
BASELINESCHEMA=newCreateMySQLSchema.sql

# Sample data
DATAPATH=`pwd`/data
DATA_CONTACT=$DATAPATH/contact.csv
DATA_ACCOUNT=$DATAPATH/account.csv

function checkerr()
{
	ERR=$?
	if [ "$ERR" != 0 ]
	then
		echo "ERROR: $MSG" >&2
		exit 1
	fi
}

function showmsg()
{
	echo $MSG
}

MY="mysql --skip-column-names --disable-auto-rehash  -u $SQLUSER --password=$SQLPWD -h $SQLSERVER"

# Drop the the database if it exists
MSG="Trying to drop database $SQLSCHEMA"
showmsg
echo "DROP SCHEMA IF EXISTS $SQLSCHEMA;" | $MY

# Re-create the database
MSG="Re-Creating database $SQLSCHEMA"
showmsg
echo "CREATE DATABASE $SQLSCHEMA;" | $MY
checkerr

# Add default schema selection to mysql command
MY="$MY $SQLSCHEMA"

# Populate the db with baseline schema
MSG="Populating db schema from $BASELINESCHEMA"
showmsg
$MY < "$BASELINESCHEMA"
checkerr

# Insert Sample Data
echo "LOAD DATA INFILE '$DATA_ACCOUNT' INTO TABLE account FIELDS TERMINATED BY ',' IGNORE 1 LINES;" | $MY
echo "LOAD DATA INFILE '$DATA_CONTACT' INTO TABLE contact FIELDS TERMINATED BY ',' IGNORE 1 LINES;" | $MY

